package app.TianQi;

import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import qiurank.Common;
import qiurank.Http;
import qiurank.Log;
import qiurank.PaFather;
import qiurank.UrlManageInterface;
import qiurank.UrlObject;

/**
 * Created by Administrator on 2016/1/13.
 */
public class TianQiPa extends PaFather {

    private JSONObject datask;
    public TianQiPa(UrlObject urlObject, UrlManageInterface _urlManage) {
        super(urlObject, (app.TianQi.UrlManage)_urlManage);
        _getDataSk();
        getBlock();
        getTomorrowBlock();
    }
    //获得天气实时的json数据
    private void _getDataSk(){
        String dl="http://d1.weather.com.cn/sk_2d/"+urlObject.params.toString()+".html?_="+new Date().getTime();
        String json=getHtmlContent(Http.get(dl, "Referer", url));
        try {
            if (json != null && !json.isEmpty()) {
                json = json.replace("var dataSK = ", "");
                datask = new JSONObject(json);
            }
        }catch (Exception e){
            /*
            Log.console("json:");
            Log.console(dl);
            Log.console("----------------------------------------");
            */
        }
    }

    /**
     * 获得非实时部分的map数据
     * @return
     */
    public Map<String,Object> nrljsonToMap(){
        if(!day_str.isEmpty()&&!night_str.isEmpty()){
            try {
                Map<String,Object>map=new HashMap<>();
                map.put("weatherID",urlObject.params.toString());
                map.put("day_temp",getTemp(day_str));
                map.put("day_wea",getWea(day_str));
                String day_wd[]=getWin(day_str);
                map.put("day_wd",day_wd[0]);
                map.put("day_ws",day_wd[1]);
                map.put("sun_up",getSunTime(day_str)/1000);
                map.put("night_temp",getTemp(night_str));
                map.put("night_wea",getWea(night_str));
                String night_wd[]=getWin(night_str);
                map.put("night_wd",night_wd[0]);
                map.put("night_ws",night_wd[1]);
                map.put("sun_down",getSunTime(night_str)/1000);
                return map;
            }catch (Exception e){

            }

        }
        return null;
    }
    public Map<String,Object> tomorrowWeather(){
        if(!tomorrow_str.isEmpty()){
            //Log.console(urlObject.params.toString());
            Map<String,Object>map=new HashMap<>();
            map.put("day_temp",getTomorrowDayTemp());
            map.put("night_temp",getTomorrowNightTemp());
            String weather=getTomorrowWeather();
            if(weather!=null) {
                map.put("weather",weather);
            }
            map.put("weatherID",urlObject.params.toString());
            map.put("create_time",Common.getAnyDateNingChen(1)/1000);
            map.put("edit_time",Common.getNowDate());
            tomorrow_str=null;
            return map;
        }
        return null;
    }
    /**
     * 获得实时部分的map数据
     * @return
     */
    public Map<String,Object> rljsonToMap(){
        JSONObject js=datask;
        if(js!=null){
            Map<String,Object>map=new HashMap<>();
            map.put("nameen",js.getString("nameen"));
            map.put("cityname",js.getString("cityname"));
            map.put("weatherID",js.getString("city"));
            map.put("temp",js.getInt("temp"));
            map.put("tempf",js.getInt("tempf"));
            map.put("wd",js.getString("WD"));
            map.put("wde",js.getString("wde"));
            map.put("ws",js.getString("WS"));
            map.put("wse",js.getString("wse"));
            map.put("sd", Integer.parseInt(js.getString("SD").replace("%", "")));
            //Log.console(js);
            //Log.console(pa.getUrl());
            map.put("rtime", Common.strToDate(js.getString("time").trim()+":00") / 1000);
            map.put("weather",js.getString("weather"));
            int aqi=0;
            try {
                aqi=js.getString("aqi").isEmpty()?0:js.getInt("aqi");
            }catch (Exception e){

            }

            map.put("aqi",aqi);
            if (aqi<=50){
                map.put("aqiname","优");
            }else if(aqi>50&&aqi<=100){
                map.put("aqiname","良");
            }else if(aqi>100&&aqi<=150){
                map.put("aqiname","轻度污染");
            }else if(aqi>150&&aqi<=200){
                map.put("aqiname","中度污染");
            }else if(aqi>200&&aqi<=300){
                map.put("aqiname","重度污染");
            }else if(aqi>300){
                map.put("aqiname","严重污染");
            }
            return map;
        }
        return null;
    }
    private String day_and_night_str="";
    private String day_str="";
    private String night_str="";
    private String tomorrow_str=new String();
    private final String day_and_night_pat="<ul class=\"clearfix\"><li>.*?</p></li></ul></div>"; //获得白天黑夜块的HTML内容
    private final String day_pat="<li><h1>.*?白天.*?<div class=\"slid\"></div></li>"; //获得白天块的HTML内容
    private final String night_pat="<li><h1>.{1,20}夜间.*?</li>"; //获得夜间块的HTML内容
    private final String temp_pat="<p class=\"tem\"><span>(.*?)</span>"; //获得温度值
    private final String wea_pat="<p class=\"wea\".*?>(.*?)</p>";  //获得天气情况
    private final String win_pat="<p class=\"win\"><i.*?</i><span.*?title=\"(.*?)\">(.*?)</span>";  //获得风情况
    private final String sun_time_pat="<p class=\"sun.*?<span>(日出|日落)(.*?)</span>";//获得日出日落时间
    private final String tomorrow_pat="<li.{0,23}><h1>.{0,4}明天.*?<div class=\"slid\"></div></li>";
    private final String tomorrow_day_temp_pat="<p class=\"tem\"><span>(.*?)℃?</span>";
    private final String tomorrow_night_temp_pat="<i>(.*?)℃</i>";
    private final String tomorrow_weather_pat="<p.{0,}class=\"wea\".*?>(.*?)</p>";
    //获得白天和夜间块
    private void getBlock(){
        Pattern pattern=Pattern.compile(day_and_night_pat);
        Matcher matcher=pattern.matcher(htmlContent);
        if(matcher.find()){
            day_and_night_str=matcher.group();
            _getDayHtml();
            _getNightHtml();
        }
    }
    private void getTomorrowBlock(){
        String tomorrow=new String ();
        tomorrow=getHtmlContent(Http.get("http://www.weather.com.cn/weather/"+urlObject.params.toString()+".shtml"));
        Pattern pattern=Pattern.compile(tomorrow_pat);
        Matcher matcher=pattern.matcher(tomorrow);
        if(matcher.find()) {
            tomorrow_str=matcher.group();
        }
        tomorrow=null;

    }
    //获得明天白天温度
    private int getTomorrowDayTemp(){
        Pattern pattern=Pattern.compile(tomorrow_day_temp_pat);
        Matcher matcher=pattern.matcher(tomorrow_str);
        if(matcher.find()){
            return Integer.parseInt(matcher.group(1));
        }
        return 0;
    }
    //获得明天夜间温度
    private int getTomorrowNightTemp(){
        Pattern pattern=Pattern.compile(tomorrow_night_temp_pat);
        Matcher matcher=pattern.matcher(tomorrow_str);
        if(matcher.find()){
            return Integer.parseInt(matcher.group(1));
        }
        return 0;
    }
    private String getTomorrowWeather(){
        Pattern pattern=Pattern.compile(tomorrow_weather_pat);
        Matcher matcher=pattern.matcher(tomorrow_str);
        if(matcher.find()){
            return matcher.group(1);
        }
        return null;
    }

    /**
     * 获得白天块内容
     */
    private void _getDayHtml(){
        Pattern pattern=Pattern.compile(day_pat);
        Matcher matcher=pattern.matcher(day_and_night_str);
        if(matcher.find()){
           day_str=matcher.group();
        }
    }
    /**
     * 获得夜间块内容
     */
    private void _getNightHtml(){
        Pattern pattern=Pattern.compile(night_pat);
        Matcher matcher=pattern.matcher(day_and_night_str);
        if(matcher.find()){
            night_str=matcher.group();
            //getTemp(night_str);
            //Log.console(night_str);

        }
    }

    /**
     * 获得白天或夜间的温度
     * @param _html  白天或夜间的HTML
     * @return  温度值
     */
    private int getTemp(String _html){
        Pattern pattern=Pattern.compile(temp_pat);
        Matcher matcher=pattern.matcher(_html);
        if(matcher.find()){
            return Integer.parseInt(matcher.group(1));
        }
        return 0;
    }
    private String getWea(String _html){
        Pattern pattern=Pattern.compile(wea_pat);
        Matcher matcher=pattern.matcher(_html);
        if(matcher.find()){
            return (matcher.group(1));
        }
        return "无数据";
    }
    private String[] getWin(String _html){
        Pattern pattern=Pattern.compile(win_pat);
        Matcher matcher=pattern.matcher(_html);
        String[] rs={"无风向信息","无风力信息"};
        if(matcher.find()){
            rs[0]=matcher.group(1);
            rs[1]=matcher.group(2);
        }
        return rs;
    }
    private long getSunTime(String _html){
        Pattern pattern=Pattern.compile(sun_time_pat);
        Matcher matcher=pattern.matcher(_html);
        if(matcher.find()){
            return (Common.strToDate(matcher.group(2).trim()+":00"));
        }
        return 0;
    }

    //获得天气实时的json数据
    public JSONObject getDataSK(){
        return datask;
    }
}
